<?php


namespace Admin\Model;
use Think\Model;
use Common\Common\ConstantUtil;


class RoleRelationModel extends Model
{
    protected $tableName = 'modules';

    public function add($post) {
        if(empty($post["id"])) return false;

        $id         = $post["id"];
        $modules    = $post["modules"];
        $date       = date("Y-m-d H:i:s");
        $model = new Model ();
        $model->startTrans();
        try{
            $model->table("role_relation")->where(["role_id"=>$id])->delete();
            if(!empty($modules)) {
                foreach($modules as $val) {
                    $data = [];
                    $data["role_id"] = $id;
                    $data["modules_func_id"] = $val;
                    $data["create_time"] = $date;
                    $model->table("role_relation")->add($data);
                }
            }
            $model->commit();
            return true;
        } catch (\PDOException $e) {
            $model->rollback();
            return false;
        }

    }
}
